define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'school_class/index' + location.search,
                    add_url: 'school_class/add',
                    edit_url: 'school_class/edit',
                    del_url: 'school_class/del',
                    multi_url: 'school_class/multi',
                    table: 'school_class',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'class_name', title: __('Class_name')},
                        {field: 'schoolclasstype.name', title: __('Class_type')},
                        {field: 'number', title: __('Number'), operate: false},
                        {field: 'admin.nickname', title: __('Admin.username')},
                        {field: 'schoolclasssubject.subject_name', title: '科目'},
                        {field: 'schoolclasssubject.total_class_hour', title: '总课时'},
                        {field: 'schoolclasssubject.use_class_hour', title: '剩余课时'},
                        {field: 'schoolclasssubject.cost', visible: Config.cost, title: '总课时费（元）'},

                        // {field: 'renew', title: __('Renew'),operate:false},
                        {
                            field: 'start_time',
                            title: __('Start_time'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'status',
                            title: __('Status'),
                            searchList: {"0": __('Status 0'), "1": __('Status 2')},
                            formatter: Table.api.formatter.status
                        },
                        /*  {
                              field: 'end_time',
                              title: __('End_time'),
                              operate: 'RANGE',
                              addclass: 'datetimerange',
                              formatter: Table.api.formatter.datetime
                          },*/
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            title: '编辑操作',
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        },
                        {
                            field: 'buttons',
                            width: "120px",
                            title: '其他操作',
                            table: table,
                            operate: false,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    visible:Config.school_class_subject_schedule,
                                    text: function(row){
                                        return '排课';
                                    },
                                    title: function (row) {
                                        return row.class_name;
                                    },
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    url: function (row) {
                                        return 'school_class_subject_schedule/index?ids=' + row.schoolclasssubject.id + '&class_id=' + row.id
                                    },
                                    callback: function (data) {
                                        //Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    }
                                },
                                {
                                    name: 'detail',
                                    text: '班级学生',
                                    visible:Config.school_calss_user,
                                    title: function (row) {
                                        return row.class_name;
                                    },
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    url: function (row) {
                                        return 'school_calss_user/index?class_id=' + row.id;
                                    },
                                    end: function () {
                                        table.bootstrapTable('refresh');
                                    }
                                },
                                {
                                    name: 'ajax',
                                    text: '结课',
                                    title: '结课',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    url: 'school_class/translate',
                                    confirm: '确认班级结课?',
                                    visible: function(row){
                                        if(!Config.translate){
                                            return Config.translate;
                                        }
                                        if(row.status == '0'){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    },
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        table.bootstrapTable('refresh');
                                    }
                                },
                                {
                                    name: 'detail',
                                    text: '备注',
                                    title: function (row) {
                                        return '用户' + row.nickname;
                                    },
                                    visible: Config.remarks,
                                    classname: 'btn btn-xs btn-success btn-click',
                                    click: function (i, row) {
                                        layer.prompt({
                                            formType: 2,
                                            title: '请输入备注',
                                            value: row.remarks,
                                            area: ['300px', '100px'] //自定义文本域宽高
                                        }, function (value, index, elem) {
                                            Fast.api.ajax({
                                                url: "school_class/remarks/ids/" + row.id,
                                                data: {'remarks': value}
                                            }, function (data, ret) {
                                                table.bootstrapTable('refresh');
                                                layer.close(index);
                                            });
                                        });
                                    },
                                    success: function (row) {

                                    }
                                },
                            ],
                            formatter: Table.api.formatter.buttons
                        }

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});